#include "bitwise.h"
#include <string>

using namespace std;

void reset(unsigned int* data, int pos) {
	if(!check(pos))
		throw "Index out of bounds exception";
	else {
		unsigned int pattern = ~(1 << pos);
		*data &= pattern;
	}
}

void reset(unsigned int* data) {
	*data = 0;
}

void set(unsigned int* data, int pos) {
	if(!check(pos))
		throw "Index out of bounds exception";
	else {
		unsigned int pattern = 1 << pos;
		*data |= pattern;
	}
}

void set(unsigned int* data) {
	*data = 0xFFFF;
}

void flip(unsigned int* data, int pos) {
	if(!check(pos))
		throw "Index out of bounds exception";
	else {
		if(test(data, pos))
			reset(data, pos);
		else
			set(data, pos);
	}

}

void flip(unsigned int* data) {
	*data = ~*data;
}

bool test(const unsigned int* data, int pos) {
	if(!check(pos))
		throw "Index out of bounds exception";
	else {
		unsigned int pattern = 1 << pos;
		pattern = *data & pattern;
		pattern >>= pos;
		return pos == 1;
	}
}

short count(const unsigned int* data) {
	int temp = *data;
	unsigned int pattern  = 1;
    short result = 0;
	for(int i=0; i<BIT_LENGTH; i++) {
		if((temp & pattern) == 1)
			result++;
		temp >>= 1;
	}
	return result;
}


bool check(int pos) {
	if((pos <= MAX_POS)&&(pos >= MIN_POS))
		return true;
	else
		return false;
}

string toString(const unsigned int* data) {
	string result = "";
	for(int i=0; i<BIT_LENGTH; i++) {
		if(test(data, i))
			result += '1';
		else
			result += '0';
	}
	return result;
}